using System.Text.RegularExpressions;
using InfiniTec.Data;

namespace InfiniTec.Expressions
{
    public class LikeExpression: CompareExpressionBase<string>
    {
        public LikeExpression(PropertyDefinition<string> property, string value) : base(property, value)
        {
        }


        protected override bool Evaluate(string value, string actualValue)
        {
            string pattern;
            Regex patternRegex;

            pattern = Regex.Escape(Value);
            pattern = pattern.Replace("%", ".*");
            pattern = pattern.Replace("_", ".");
            pattern = pattern.Replace("\\[\\^", "[^");
            pattern = pattern.Replace("\\[", "[");
            pattern = pattern.Replace("\\]", "]");

            patternRegex = new Regex(pattern);

            return patternRegex.IsMatch(actualValue);
        }
    }
}
